import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    //收缩菜单模块
    isCollapse:false,
    //菜单选项卡模块
    activeTabName: "/System/Users,用户管理,el-icon-s-custom",
    tabList: [
      {
        label: '工作台',
        name: '/System/Workbench,工作台,el-icon-s-grid',
        icon: 'el-icon-s-grid',
        disabled: false,
        closable: false,
        component: resolve => require(['../views/System/Workbench'], resolve)
      },
      {
        label: '用户管理',
        name: '/System/Users,用户管理,el-icon-s-custom',
        icon: 'el-icon-s-custom',
        disabled: false,
        closable: true,
        component: resolve => require(['../views/System/Users'], resolve)
      }
    ]
  },
  mutations: {
    //改变收缩
    changeCollapse(state,v){
      state.isCollapse = v;
    },

    //添加选项卡
    addTab(state, index) {
      let name = index.split(',')[1]; //拿到选项卡名称
      let url = index.split(',')[0]; //路径
      let icon = index.split(',')[2]; //路径
      //判断tablist里面有没有一样的name ,如果没有把数据添加到tablist
      let tab = state.tabList.filter(f => f.name == index);
      if (tab == 0) {
        let component = resolve => require([`../views${url}`], resolve)
        state.tabList.push({
          label: name,
          name: index,
          icon: icon,
          disabled: false,
          closable: true,
          component: component
        })
      } else {
        this.commit('changeTab', index); //切换刷新页面
      }
      state.activeTabName = index; //选中指定的选项卡
    },
    //移除选项卡，判断如果后面还存在选项卡则选,如果没有则选中前一个选项卡
    closeTab(state, name) {
      if (name == state.activeTabName) {
        let tab = state.tabList.filter(f => f.name == name)[0];
        let index = state.tabList.indexOf(tab);//获取选项卡位置
        if (index != state.tabList.length - 1) {
          state.activeTabName = state.tabList[index + 1].name;
        } else {
          state.activeTabName = state.tabList[index - 1].name;
        }
      }
      state.tabList = state.tabList.filter(f => f.name != name);//赋值
    },
    //选项卡切换  刷新数据
    changeTab(state, name) {
      let tab = state.tabList.filter(f => f.name == name)[0];
      let url = name.split(',')[0]; //路径
      //异步加载刷新页面
      tab.component = resolve => require([`../views${url}`], resolve);
    }
  },
  actions: {
  },
  modules: {
  }
})
